package com.hmkj.quote.config;

import com.hmkj.boot.utils.SpringContextUtils;
import com.hmkj.core.api.ufx.call.QuotationCall;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.po.commo.Commo;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.risk.RiskParamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 初始化数据
 */
@Slf4j
@Component
public class Init implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
        // 初始化行情服务器连接
        QuotationCall.getInstance().initT2();

        // 订阅行情主推信息
        QuotationCall.getInstance().reqSubQuotation();

        // 国内期货设置主推代码
        setPushCode();
        log.info("国内期货设置主推代码成功！");
    }


    private void setPushCode() {
        RedisService redisService = SpringContextUtils.getBean(RedisService.class);
        RiskParamService riskParamService = SpringContextUtils.getBean(RiskParamService.class);
        List<Commo> list = redisService.getList(CacheID.COMMO_LIST, Commo.class);
        if (CollectionUtils.isNotEmpty(list)) {
            for (Commo commo : list) {
                if (commo.getType().intValue() == Commo.TYPE.T2.code) {
                    try {
                        String contract = riskParamService.getContractNo(commo.getId());
                        QuotationCall.getInstance().setPushCode(commo.getCode() + contract);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
